﻿using DeviceMaintainGIS.Entity;
using DeviceMaintainGIS.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceMaintainGIS.BLL
{
    /// <summary>
    /// 公共方法
    /// </summary>
    public class CommonsService
    {
        /// <summary>
        /// 图片删除
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns></returns>
        public ReturnMessageVM DelImageFile(string strUrl)
        {
            ReturnMessageVM rm = new ReturnMessageVM();
            rm.Succeed = true;
            try
            {
                using (CYOADBEntities Entities = new CYOADBEntities())
                {
                    Image_Tab it = Entities.Image_Tab.Where(i => i.ImagePath == strUrl).FirstOrDefault();
                    if (it != null)
                    {
                        Entities.Image_Tab.Add(it);
                        Entities.Entry<Image_Tab>(it).State = System.Data.Entity.EntityState.Deleted;
                        int code = Entities.SaveChanges();
                        if (code < 1)
                        {
                            rm.Succeed = false;
                            return rm;
                        }
                    }



                    try
                    {
                        if (File.Exists(strUrl))
                        {
                            File.Delete(strUrl);
                        }


                    }
                    catch (Exception ee)
                    {


                    }
                }
            }
            catch (Exception ee)
            {
                rm.Succeed = false;
                rm.ErrorCode = "2";
            }
            return rm;
        }
    }
}
